package book.zone.model;

import book.zone.auth.User;
import book.zone.helpers.PasswordCrypt;
import book.zone.log.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by IntelliJ IDEA.
 * User: ndemeshchenko
 * Date: May 14, 2010
 * Time: 4:42:57 PM
 */
public class SignUpModel {
    private User user;
    int rows;

    public SignUpModel(){}
    public SignUpModel(User usr){
        this.user = usr;
    }

    /*
     * Check if new user already exist
     * return TRUE if duplicates does NOT found,
     * and vice versa - FALSE.
     */
    private boolean duplicateCheck(){
        return false;
    }

    /*
     * Check for completeness of all fields.
     */
    private boolean checkFields(){
        return !(user.getFirstName().equals(null) &&
                 user.getLastName().equals(null) &&
                 user.getEmail().equals(null) &&
                 user.getCity().equals(null) &&
                 user.getPasswd().equals(null)
                );
    }

    /*
     * Insert new user method runned after duplicateCheck method,
     * return TRUE if insert passed successfully.
     */
    private boolean insertNewUser(){
        String insertStatement = "INSERT INTO USERS"
                               + " (firstName, lastName, email, passwd, city)"
                               + "VALUES "
                               + "('" + user.getFirstName()   + "',"
                               + "'" + user.getLastName()   + "',"
                               + "'" + user.getEmail()   + "',"
                               + "'" + PasswordCrypt.encrypt(user.getPasswd())   + "',"
                               + "'" + user.getCity() + "')";
        Logger.out(insertStatement);

        try{
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/bookzone");
            if (ds != null) {
                Connection conn = ds.getConnection();
                if(conn != null){
                    Logger.out("Got Connection " + conn.toString());
                    Statement stmt = conn.createStatement();
                    rows = stmt.executeUpdate(insertStatement);
                    Logger.out(rows);
                    stmt.close();
                    conn.close();
                }
            }
            else{
                Logger.out("Not Connected");
            }
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /*
     * Check for all fields first before start inserting proceed.
     */
    public boolean doInsert() {
        if(checkFields()){
            this.insertNewUser();
            return true;
        }
        else{
            return false;
        }
    }
}
